package com.ymx.darling.dialect;

import com.ymx.darling.bean.sql.BoundSql;
import com.ymx.darling.bean.sql.SqlStatement;

import java.util.List;

/**
 * mysql 方言
 * @author 兴哥的小新
 */
public class MySqlDialect implements Dialect{

    private String splitSql(String sql) {
        return sql+ " LIMIT ?,? ";
    }

    @Override
    public String getDataBaseType() {
        return "mysql";
    }

    @Override
    public void paginate(BoundSql boundSql, int pageNum, int pageSize) {
        List<Object> rightParameters = boundSql.getRightParameters();
        int newPageNum = (pageNum - 1) * pageSize;
        rightParameters.add(newPageNum);
        rightParameters.add(pageSize);
        boundSql.setSql(this.splitSql(boundSql.getSql()));
    }

    @Override
    public void limit(SqlStatement statement, long limit, long offset) {
        List<Object> paramList = statement.getParamList();
        paramList.add(offset);
        paramList.add(limit);

        String pageSql = this.splitSql(statement.getSql());
        statement.setSql(pageSql);
    }

    @Override
    public String _if(String condition, Object trueValue, Object falseValue) {
        return String.format("IF (%s, %s, %s)", condition, trueValue, falseValue);
    }
}
